﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Windows.Forms;

namespace SmartCollection
{
    public class ThoiTrangDanny
    {
       
        SmartCollectionBase smart = new SmartCollectionBase();
     // lay link cua trang tiep theo
        public List<string> BasicLoad(string s,int j, string s1,string s2,List<string> arrayLink)
        {
            
            var web = new HtmlWeb();
            var doc = web.Load(s);
            //  doc.Load(new StringReader(s));
            if (doc.ParseErrors != null && doc.ParseErrors.Count() > 0)
            {


            }
            else
            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    var listPro = doc.DocumentNode.SelectNodes(s1);

                    if (listPro != null)
                    {
                        //pro.Link = names.Attributes[0].Value;
                        // Console.WriteLine(names.Attributes[0].Value);
                        // duyet danh sach
                        for (int i=j;i<listPro.Count(); i++)
                        {
                         
                            //  var webPro = new HtmlWeb();
                            //  var proDoc=  webPro.Load(node.InnerHtml);
                            HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

                            proDoc.LoadHtml(listPro[i].InnerHtml);
                            //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");

                            var names = proDoc.DocumentNode.SelectSingleNode(s2);
                            if (names != null)
                            {
                                // lay ra link
                                string item = names.Attributes[0].Value;
                                //  WriteFile(names.Attributes[0].Value);
                                arrayLink.Add(item);

                            }
                        }
                    }
                }
            }
            return arrayLink;
        }
        public List<string> LoadPage(string s)
        {
            List<string> arrayPage=new List<string>();
            //var web = new HtmlWeb();
            //var doc = web.Load(s);
         return   BasicLoad(s,2, "//ul[@class='page' and @style='margin:10px;']//li", "//a",arrayPage);
     
        }


        // lay link cua san pham
        public List<string> LoadHtmlOnePage(string s)
        {
            //   HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //   doc.OptionFixNestedTags = true;
            List<string> arrayLinkPro = new List<string>();
            //var web = new HtmlWeb();
            //var doc = web.Load(s);
            return BasicLoad(s,0, "//div[@class='product']", "//p//a", arrayLinkPro);

        }

        public bool LoadProduct(string s, int manuId, int cateId)
        {
            var web = new HtmlWeb();
            var doc = web.Load(s);
      
            List<ProductBase> arrayPro = new List<ProductBase>();
        
            if (doc.ParseErrors != null && doc.ParseErrors.Count() > 0)
            {

                return false;
            }
            else
            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product

                    var listPro = doc.DocumentNode.SelectNodes("//div[@class='row_right']");

                    if (listPro != null)
                    {
                        //pro.Link = names.Attributes[0].Value;
                        // Console.WriteLine(names.Attributes[0].Value);
                        // duyet danh sach
                        //   ProductBase pro= new ProductBase();
                        ProductBase pro = new ProductBase();

                        //ClassBase pro2 = new ClassBase();

                        pro.Name = listPro[0].InnerHtml.ToCutString();
                        pro.Code = listPro[1].InnerText.ToCutString();
                        pro.Price = doc.DocumentNode.SelectSingleNode("//div[@class='row_right info_bold']").InnerText.ToCutString();
                        pro.ShortDescription = pro.Name;
                        doc.DocumentNode.SelectSingleNode("//div[@style='padding:10px']//p");

                        var listImg = doc.DocumentNode.SelectNodes("//div[@style='padding:10px;']//p//img");
                        List<string> arrayImg = new List<string>();
                        if (listImg != null)
                            for (int i = 0; i < listImg.Count(); i++)
                            {
                                string s1 = "http://thoitrangdanny.com" + listImg[i].Attributes[1].Value;

                                arrayImg.Add(s1);
                            }
                        // hinh ko co duong dan tuyet doi 
                        //var des = doc.DocumentNode.SelectSingleNode("//div[@style='padding:10px;']");
                        //pro.FullDescription = des.InnerHtml.ToString();

                        string des = "";
                        des += "<div align='center'>";
                        for (int i = 0; i < arrayImg.Count(); i++)

                            des += "<p><img src='" + arrayImg[i] + "' /></p>";

                        des += "</div>";
                        pro.FullDescription = des;
                        List<string> listUrlImgPc= new List<string>();
                        listUrlImgPc= smart.PhotoDownloader(arrayImg, "e:\\test", pro.Code);
                        if (listUrlImgPc == null || arrayImg == null)
                            return false;
                        {
                            arrayPro.Add(pro);
                           
                            Product dbPro= smart.SaveProduct(pro);
                            smart.SaveToUrlRecord(dbPro.Id, "Product", dbPro.Name.convertToUnSign3(), true, 0);
                            for (int i = 0; i < listUrlImgPc.Count(); i++)
                            {
                               byte[] picBinary= smart.ImageToByteArrayFromFilePath(listUrlImgPc[i]);
                                Picture picture= smart.SaveToPicture(picBinary, "image/jpg", dbPro.Name, false);
                              smart.SaveToProduct_Pic(dbPro.Id, picture.Id, 0);
                              
                            }
                            smart.SaveToProduct_Category(dbPro.Id, cateId, false, 0);
                            smart.SaveToProduct_Manu(dbPro.Id, manuId, false, 0);
                            return true;
                        }

                    }

                }
                return true;
            }
          


        }
        public void Run(string url, int manuId,int cateId)
        {
            List<string> arrayPage = new List<string>();
            List<string> arrayLinkPro = new List<string>();
         
            
            arrayPage = LoadPage(url);
            arrayPage.Add(url);
          
            for (int i = 0; i < arrayPage.Count(); i++)
            {
               arrayLinkPro =  LoadHtmlOnePage(arrayPage[i]);
               for (int j = 0; j < arrayLinkPro.Count(); j++)
               {
                   LoadProduct(arrayLinkPro[j], manuId, cateId);
                  
               }
              
                        
            }
            MessageBox.Show("\nThanh cong!");
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");


        }
    }

  
}
